<?php

namespace app\admin\controller;

use think\Controller;
use app\admin\model\Menus as M;

//菜单
class Menus extends Controller
{
    public function index()
    {
        $m     = new M();
        $menus = $m->getAllMenus();
        $this->assign('menus', $menus);
        return $this->fetch();
    }


    /*
     * 获取josn数据
     * 
     */
    public function get_josn()
    {
        $m     = new M();
        $menus = $m->getAllMenus();
        $lists = ['code' => 0, 'msg' => '', 'data' => []];
        foreach ($menus as $k => $v) {
            //父级目录
            if ($v['pid'] == 0) {
                $lists['data'][] = $v;
                foreach ($menus as $val) {
                    if ($val['pid'] == 0) {
                        continue;
                    }
                    //子级目录
                    if ($val['pid'] > 0 && $val['pid'] == $v['id']) {
                        $space           = "";
                        $space           .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|--";
                        $val['name']     = $space . $val['name'];
                        $lists['data'][] = $val;
                    }
                }
            }
        }
        return $lists;
    }


    /*
     * 添加页面
     * 
     */
    public function add()
    {
        $m     = new M();
        $menus = $m->get2leveMenu();
        $this->assign('menus', $menus);
        return $this->fetch();
    }


    /*
     * 添加提交
     * 
     */
    public function add_submit()
    {
        $m = new M();
        return $m->add();
    }


    /*
     * 修改页面
     * 
     */
    public function edit()
    {
        $m     = new M();
        $menus = $m->getMainMenus();
        $this->assign('menus', $menus);
        $s = $m->getById(input('id'));
        $this->assign('id', input('id'));
        $this->assign('s', $s);
        return $this->fetch();
    }


    /*
     * 修改提交
     * 
     */
    public function edit_submit()
    {
        $m = new M();
        return $m->edit();
    }


    /*
     * 删除
     * 
     */
    public function del_submit()
    {
        $m  = new M();
        $rs = $m->del();
        //日志
        $id   = input('id');
        $name = session('my_self')['name'];
        $des  = $name . "【删除】了编号为【" . $id . "】的菜单";
        add_log(2, session('my_self')['id'], $des);
        if ($rs[0] == 1) {
            $this->success($rs[1], url('menus/index'));
        }
        else {

        }
    }
}
